package org.polaris.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.polaris.authority.annotation.PolarisPermission;
import org.polaris.basic.tool.AjaxResult;
import org.polaris.basic.tool.PageResult;
import org.polaris.org.domain.Employee;
import org.polaris.org.query.EmployeeQuery;
import org.polaris.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "员工的API",description = "员工相关CRUD")
@RequestMapping("/employee")
@RestController
@PolarisPermission(name="员工管理",descs = "员工相关CRUD")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @ApiOperation(value = "查询全部")
    @GetMapping
    @PolarisPermission(name="查询全部",descs = "查询全部")
    public AjaxResult selectAll(){
        try {
            List<Employee> employees = employeeService.selectAll();
            return AjaxResult.result().setObj(employees);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @ApiOperation(value = "通过ID查询")
    @GetMapping("/{id}")
    @PolarisPermission(name="通过ID查询",descs = "通过ID查询")
    public AjaxResult selectById(@ApiParam(name = "ID属性",required = true) @PathVariable("id") Long id){
        try {
            Employee employee = employeeService.selectById(id);
            return AjaxResult.result().setObj(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @ApiOperation(value = "通过ID删除")
    @DeleteMapping("/{id}")
    @PolarisPermission(name="通过ID删除",descs = "通过ID删除")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            employeeService.deleteById(id);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }
    }
    @ApiOperation(value = "新增&修改,存在ID则修改反之则新增")
    @PutMapping
    @PolarisPermission(name="保存",descs = "新增&修改,存在ID则修改反之则新增")
    public AjaxResult save(@RequestBody Employee employee){
        if (StringUtils.isEmpty(employee.getId())){
            try {
                employeeService.add(employee);
                return AjaxResult.result();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
            }

        }else {
            try {
                employeeService.update(employee);
                return AjaxResult.result();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
            }
        }
    }
    @ApiOperation(value = "批量删除")
    @PolarisPermission(name="批量删除",descs = "批量删除")
    @PatchMapping
    //除get请求其他请求传参应该加上requestBody否侧无数据
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.batchDelete(ids);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @ApiOperation(value = "分页")
    @PostMapping
    @PolarisPermission(name="分页",descs = "分页")
    public AjaxResult paging(@RequestBody EmployeeQuery employeeQuery){
        try {
            PageResult paging = employeeService.paging(employeeQuery);
            System.out.println(paging);
            return AjaxResult.result().setObj(paging);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }

    }


}
